package evol;

import java.util.Random;

import Oracle.Connector;

import ia.battle.camp.BattleField;
import ia.battle.camp.BattleFieldListener;
import ia.battle.camp.FieldCell;
import ia.battle.camp.Warrior;
import ia.battle.camp.WarriorManager;
import ia.exceptions.RuleException;

public class ExampleEvol {
	private Connector oracle = new Connector();
	private BattleField bf;
	private WarriorManager win = null;
	public static int cnt = 0;
	ManagerEvol wm1 = new ManagerEvol(null);
	ManagerEvol wm2 = new ManagerEvol(null);
	Random r = new Random();
	private static ExampleEvol instance = new ExampleEvol();

	private ExampleEvol() {
	}

	public static void main(String[] args) {

		ExampleEvol eV = ExampleEvol.getInstance();

		try {
			eV.newFight();
		} catch (RuleException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void newFight() throws RuleException {
		synchronized (this) {
			bf = BattleField.getInstance();
			if (wm1 == win) {
				wm2.changeWarrior(win.getNextWarrior());
				System.out.println("gano 1");

			} else if (wm2 == win) {
				wm1.changeWarrior(win.getNextWarrior());
				System.out.println("gano 2");

			} else {
				System.err.println("Repasar");
			}
			wm1 = new ManagerEvol(wm1.getW());
			wm2 = new ManagerEvol(wm2.getW());

			bf.addWarriorManager(wm1);
			bf.addWarriorManager(wm2);

			bf.addListener(new BattleFieldListener() {

				@Override
				public void startFight() {
					// TODO Auto-generated method stub

				}

				@Override
				public void figthFinished(WarriorManager winner) {
					// TODO Auto-generated method stub
					try {
						ExampleEvol.getInstance().setWinner(winner);
					} catch (RuleException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				@Override
				public void tickLapsed(long tick) {
					// TODO Auto-generated method stub
				}

				@Override
				public void turnLapsed(long tick, int turnNumber,
						Warrior warrior) {
					// TODO Auto-generated method stub
				}

				@Override
				public boolean continueFighting() {
					// TODO Auto-generated method stub
					return true;
				}

				@Override
				public void warriorAttacked(Warrior attacked, Warrior attacker,
						int damage) {
					// TODO Auto-generated method stub

				}

				@Override
				public void warriorMoved(Warrior warrior, FieldCell from,
						FieldCell to) {
					// TODO Auto-generated method stub

				}

			});

			bf.fight();
		}
	}

	public static ExampleEvol getInstance() {
		return instance;
	}

	public void setWinner(WarriorManager winner) throws RuleException {
		synchronized (this) {
			this.win = winner;
			System.out.println(" Nombre: " + win.getNextWarrior().getName()
					+ " health: " + win.getNextWarrior().getInitialHealth()
					+ " defense: " + win.getNextWarrior().getInitialDefense()
					+ " strenght: " + win.getNextWarrior().getInitialStrength()
					+ " speed: " + win.getNextWarrior().getInitialSpeed()
					+ " Range: " + win.getNextWarrior().getInitialRange());

			oracle.setData(cnt++, win.getNextWarrior().getInitialHealth(), win
					.getNextWarrior().getInitialDefense(), win.getNextWarrior()
					.getInitialStrength(), win.getNextWarrior()
					.getInitialSpeed(), win.getNextWarrior().getInitialRange());

			bf.showResult();
			newFight();
		}
	}
}
